//
//  ViewController.m
//  ballGame
//
//  Created by happy on 2023/6/21.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *timesBtn;
@property (weak, nonatomic) IBOutlet UITextField *timesInput;
@property (weak, nonatomic) IBOutlet UILabel *label840;
@property (weak, nonatomic) IBOutlet UILabel *label831;
@property (weak, nonatomic) IBOutlet UILabel *label822;
@property (weak, nonatomic) IBOutlet UILabel *label750;
@property (weak, nonatomic) IBOutlet UILabel *label660;
@property (weak, nonatomic) IBOutlet UILabel *label741;
@property (weak, nonatomic) IBOutlet UILabel *label651;
@property (weak, nonatomic) IBOutlet UILabel *label642;
@property (weak, nonatomic) IBOutlet UILabel *label633;
@property (weak, nonatomic) IBOutlet UILabel *label552;
@property (weak, nonatomic) IBOutlet UILabel *label444;
@property (weak, nonatomic) IBOutlet UILabel *label345;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@property (nonatomic, strong) NSArray *allBalls;
@property (nonatomic, assign) NSInteger times;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.timesInput endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.times = 1;
    self.timesInput.placeholder = @"请输入次数";
    [self.timesInput addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [self resetTimes];

    // step1.准备红黄蓝球各8枚
    NSMutableArray * reds = @[].mutableCopy;
    NSMutableArray * yellows = @[].mutableCopy;
    NSMutableArray * blues = @[].mutableCopy;
    for (int i = 0; i < 8; i++) {
        [reds addObject: @"red"];
        [yellows addObject: @"yellow"];
        [blues addObject: @"blue"];
    }
    
    // step2.将球全放一个袋子里
    NSMutableArray * balls = [NSMutableArray arrayWithCapacity:24];
    [balls addObjectsFromArray:reds];
    [balls addObjectsFromArray:yellows];
    [balls addObjectsFromArray:blues];
    self.allBalls = balls.copy;
}

- (NSArray *)getBalls {
    // step3.从袋子中随机取出12个球
    NSMutableArray * copyBalls = self.allBalls.mutableCopy;
    NSMutableArray * getReds = [NSMutableArray array];
    NSMutableArray * getYellows = [NSMutableArray array];
    NSMutableArray * getBlues = [NSMutableArray array];
    int count = 24;
    while (24 - count < 12) {
        int r = arc4random_uniform(count);
        NSString * obj = [copyBalls objectAtIndex:r];
        if ([obj isEqualToString:@"red"]) {
            [getReds addObject: obj];
        } else if ([obj isEqualToString:@"yellow"]) {
            [getYellows addObject: obj];
        } else if ([obj isEqualToString:@"blue"]) {
            [getBlues addObject: obj];
        }
        [copyBalls removeObjectAtIndex:r];
        count--;
    }
//    NSLog(@"\nred=%ld\nyellow=%ld\nblue=%ld\n", getReds.count, getYellows.count, getBlues.count);
    return @[@(getReds.count), @(getYellows.count), @(getBlues.count)];
}

- (void)timesGetBalls {
    if (self.times == 0) return;
    NSMutableArray * alls = [NSMutableArray array];
    for (int i = 0; i < self.times; i++) {
        [alls addObject:[self getBalls]];
    }
    [self handleData:alls.copy];
}

- (void)handleData:(NSArray *)array {
    NSInteger num840 = 0;
    NSInteger num831 = 0;
    NSInteger num822 = 0;
    NSInteger num750 = 0;
    NSInteger num660 = 0;
    NSInteger num741 = 0;
    NSInteger num651 = 0;
    NSInteger num642 = 0;
    NSInteger num633 = 0;
    NSInteger num552 = 0;
    NSInteger num444 = 0;
    NSInteger num345 = 0;
    for (NSArray * arr in array) {
        if ([self array:arr containsObjects:@[@8, @4, @0]]) {
            num840++;
        } else if ([self array:arr containsObjects:@[@8, @3, @1]]) {
            num831++;
        } else if ([self array:arr containsObjects:@[@8, @2, @2]]) {
            num822++;
        } else if ([self array:arr containsObjects:@[@7, @5, @0]]) {
            num750++;
        } else if ([self array:arr containsObjects:@[@6, @6, @0]]) {
            num660++;
        } else if ([self array:arr containsObjects:@[@7, @4, @1]]) {
            num741++;
        } else if ([self array:arr containsObjects:@[@6, @5, @1]]) {
            num651++;
        } else if ([self array:arr containsObjects:@[@6, @4, @2]]) {
            num642++;
        } else if ([self array:arr containsObjects:@[@6, @3, @3]]) {
            num633++;
        } else if ([self array:arr containsObjects:@[@5, @5, @2]]) {
            num552++;
        } else if ([self array:arr containsObjects:@[@4, @4, @4]]) {
            num444++;
        } else if ([self array:arr containsObjects:@[@3, @4, @5]]) {
            num345++;
        }
    }
    NSInteger num = num840+num831+num822+num750+num660+num741+num651+num642+num633+num552+num444+num345;
    NSInteger moneyIn = num840*100+num831*10+num822*10+num750*20+num660*20+num741*2+num651*1+num642*1+num633*1+num552*1+num444*1;
    NSInteger moneyOut = num345*10;
    NSString * sign = @"%";
    NSLog(@"all num=%ld", num);
    self.label840.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num840, (num840*100.0)/(self.times*1.0), sign];
    self.label831.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num831, (num831*100.0)/(self.times*1.0), sign];
    self.label822.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num822, (num822*100.0)/(self.times*1.0), sign];
    self.label750.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num750, (num750*100.0)/(self.times*1.0), sign];
    self.label660.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num660, (num660*100.0)/(self.times*1.0), sign];
    self.label741.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num741, (num741*100.0)/(self.times*1.0), sign];
    self.label651.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num651, (num651*100.0)/(self.times*1.0), sign];
    self.label642.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num642, (num642*100.0)/(self.times*1.0), sign];
    self.label633.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num633, (num633*100.0)/(self.times*1.0), sign];
    self.label552.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num552, (num552*100.0)/(self.times*1.0), sign];
    self.label444.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num444, (num444*100.0)/(self.times*1.0), sign];
    self.label345.text = [NSString stringWithFormat:@"%ld次  %.2lf%@", num345, (num345*100.0)/(self.times*1.0), sign];
    
    NSInteger all = moneyIn - moneyOut;
    NSString * str = all > 0 ? @"赚" : @"亏";
    if (all == 0) str = @"持平";
    self.resultLabel.text = [NSString stringWithFormat:@"您一共玩了%ld次，得%ld元，交%ld元\n%@%ld元，其他点数%ld次白玩。", self.times, moneyIn, moneyOut, str, all, self.times-num];
}

- (BOOL)array:(NSArray*)arr1 containsObjects:(NSArray*)arr2 {
    NSArray *ascArr1 = [self arraySortASC:arr1];
    NSArray *ascArr2 = [self arraySortASC:arr2];
    if ([ascArr1[0] integerValue] == [ascArr2[0] integerValue] &&
        [ascArr1[1] integerValue] == [ascArr2[1] integerValue] &&
        [ascArr1[2] integerValue] == [ascArr2[2] integerValue]) return YES;
    return NO;
}


- (NSArray *)arraySortASC:(NSArray *)array {
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    return result;
}

- (void)resetTimes {
    self.label840.text = @"0次 0%";
    self.label831.text = @"0次 0%";
    self.label822.text = @"0次 0%";
    self.label750.text = @"0次 0%";
    self.label660.text = @"0次 0%";
    self.label741.text = @"0次 0%";
    self.label651.text = @"0次 0%";
    self.label642.text = @"0次 0%";
    self.label633.text = @"0次 0%";
    self.label552.text = @"0次 0%";
    self.label444.text = @"0次 0%";
    self.label345.text = @"0次 0%";
}

- (void)textFieldChanged:(UITextField *)textField{
    self.times = textField.text.integerValue;
    [self.timesBtn setTitle:[NSString stringWithFormat:@"%@次", textField.text] forState:UIControlStateNormal];
}

- (IBAction)reset:(id)sender {
    [self.timesInput endEditing:YES];
    self.times = 1;
    [self resetTimes];
    self.resultLabel.text = @"";
}
- (IBAction)once:(id)sender {
    [self.timesInput endEditing:YES];
    self.times = 1;
    NSArray * alls = [self getBalls];
    [self handleData:@[alls.copy]];
}
- (IBAction)times:(id)sender {
    self.times = [self.timesInput.text integerValue];
    [self.timesInput endEditing:YES];
    [self timesGetBalls];
}

@end
